package com.csdj.t309.controller;

import com.csdj.t309.common.MessageCode;
import com.csdj.t309.common.WebResult;
import com.csdj.t309.entity.TAddress;
import com.csdj.t309.entity.TSysRole;
import com.csdj.t309.entity.TSysUser;
import com.csdj.t309.service.IUserService;
import com.csdj.t309.service.RedisService;
import com.csdj.t309.service.UserService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping("/test")
public class TestController {

    private Lock lock = new ReentrantLock();

    private volatile int balance = 10;

    private Logger log = Logger.getLogger("testController");
    @Resource
    private IUserService userService;
    @Resource
    private RedisService redisService;


    @GetMapping("/sale")
    public WebResult sale(){
        TSysUser user = new TSysUser();
        user.setRealName("cloud");
        user.setAccount("eadfs");
        user.setCreatedTime(new Date());
        WebResult result = new WebResult();
        result.setCode(1);
        result.setMsg("成功");
        result.putData("data",user)
                .putData("key","key")
                .putData("中文","姓名")
                .putData("name","cloud");
        return result;
    }


    @GetMapping("/converter")
    public String convetorTest(Date date){
        System.out.println(date.toString());
        return date.toString();
    }

    @GetMapping("/converter/user")
    public String convetorUser(@RequestParam("user") TSysUser user){
        System.out.println(user.toString());
        return user.toString();
    }

    @PostMapping("/conv/test")
    public String convertorTest(@RequestBody TSysRole tSysRole) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String s = objectMapper.writeValueAsString(tSysRole);
        log.debug(s);
        return s;
    }

    @GetMapping("/converter/address")
    public String convetorAddress(@RequestParam("addr") TAddress address){
        log.debug(address.toString());
        return address.toString();
    }

    @GetMapping("/lang")
    public WebResult lang(){
        userService.login("aaa","dfsdf");
        return new WebResult(MessageCode.SUCCESS);
    }

    @GetMapping("/msg")
    public WebResult pubish(String msg){
        redisService.publish("test",msg);
        return new WebResult(MessageCode.SUCCESS);
    }
}
